<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../store/auth.js'
import { User, Lock } from '@element-plus/icons-vue'
// import {ElMessage} from "element-plus";

const router = useRouter()
const data = reactive({
  username: '',
  password: ''
})
const HandleLogin =async () =>{
   try {
     if (!data.username || !data.password) {
      ElMessage.warning("用户名和密码不能为空")
      return
    }
    const response = await fetch('http://localhost:8000/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        username: data.username,  // 不需要 .value，因为这是 reactive 对象
        password: data.password
      })
    })

    const result = await response.json()
     console.log(result)

    if (response.ok) {
      // 先存储到localStorage
      localStorage.setItem('token', result.token);
      localStorage.setItem('user_type', result.user_type);
      localStorage.setItem('user_id', result.user_id);
      
      // 再更新auth store
      const auth = useAuthStore()
      auth.setLoginData({
        token: result.token,
        role: result.user_type,
        userId: result.user_id
      })
      
      console.log('登录成功，用户信息已存储:', {
        localStorage: {
          token: localStorage.getItem('token'),
          user_type: localStorage.getItem('user_type'),
          user_id: localStorage.getItem('user_id')
        },
        authStore: {
          token: auth.token,
          role: auth.role,
          userId: auth.userId
        }
      });
      
      ElMessage.success('登录成功')
      router.push('/') // 登录成功后跳转
    } else {
       ElMessage.error(result.detail || '用户名密码不正确！')
    }
  } catch (error) {
     console.error(error);
     ElMessage.error('网络错误')
  }
}

</script>

<template>
  <div class="login-container">
    <div class="login-background"></div>
    <div class="login-content">
      <el-card class="login-card">
        <div class="logo-container">
          <img src="../assets/images/logo.png" alt="WIN-WIN招聘平台" class="logo-image"/>
        </div>
        <h2 class="login-title">欢迎登录双赢招聘平台</h2>
        <div class="input-group">
          <el-input
            v-model="data.username"
            placeholder="请输入用户名"
            clearable
            class="custom-input"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </div>
        <div class="input-group">
          <el-input
            v-model="data.password"
            type="password"
            placeholder="请输入密码"
            show-password
            class="custom-input"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </div>
        <div class="button-group">
          <el-button type="primary" class="action-btn" @click="HandleLogin">登录</el-button>
        </div>
        <div class="login-footer">
          <el-button type="text" @click="router.push('/register')">注册账号</el-button>
          <el-button type="text" @click="router.push('/')">返回首页</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  background-color: transparent;
}

.login-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  //background-image: url('../assets/images/login-bg.png');
  background-size: cover;
  background-position: center;
  filter: brightness(0.7);
  z-index: -2;
}

.login-content {
  width: 100%;
  max-width: 1200px;
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 1;
}

.login-card {
  width: 400px;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(10px);
  animation: fadeIn 0.5s ease-out;
  position: relative;
  z-index: 1;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.logo-image {
  width: 120px;
  height: auto;
  transition: transform 0.3s;
}

.logo-image:hover {
  transform: scale(1.05);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
  font-weight: 600;
  font-size: 24px;
  background: linear-gradient(45deg, #409EFF, #36cfcf);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.input-group {
  margin-bottom: 25px;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 8px 15px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  
  &:hover, &:focus-within {
    box-shadow: 0 2px 12px 0 rgba(64, 158, 255, 0.2);
  }
}

.button-group {
  margin-top: 30px;
}

.action-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(45deg, #409EFF, #36cfcf);
  border: none;
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  }
}

.login-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
  
  .el-button {
    color: #606266;
    transition: all 0.3s;
    
    &:hover {
      color: #409EFF;
      transform: translateX(5px);
    }
  }
}
</style>